package org.example.entity;

import lombok.Data;
import java.util.ArrayList;

@Data
public class GraphBean {

    /**
     * 节点数量
     */
    int nodeNum;

    /**
     * 边数量
     */
    int edgeNum;

    /**
     * 边数组
     */
    ArrayList<EdgeBean> edges;

    /**
     * 节点数组
     */
    ArrayList<NodeBean> nodes;

    /**
     * next 数组，维护下标idx为源点的节点
     */
    ArrayList<ArrayList<Integer>> next;

    public GraphBean() {
        this.nodeNum = 0;
        this.edgeNum = 0;
        edges = new ArrayList<>();
        nodes = new ArrayList<>();
        next = new ArrayList<>();
    }

    public ArrayList<NodeBean> addNode(NodeBean node) {
        if (node.getId() == -1) {
            node.setId(nodeNum ++);
        }
        nodes.add(node);

        return nodes;
    }

    public ArrayList<EdgeBean> addEdge(EdgeBean edge) {
        edge.setEdgeId(edgeNum ++);
        edges.add(edge);

        return edges;
    }

    public void addNextById(int nodeId, int edgeId) {
        while (next.size() <= nodeId) {
            next.add(new ArrayList<>());
        }

        next.get(nodeId).add(edgeId);
    }

}
